package com.example.picture_sharing_system;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.NetworkOnMainThreadException;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import utils.BaseResponse;
import utils.Constants;
import utils.MyWorksListAdapter;
import utils.Works;


public class MyWorksListFragment extends Fragment {

    private RecyclerView rv_my_works_list = null;
    private MyWorksListAdapter myWorksListAdapter = null;
    private List<Works> myWorksData;
    private int userid;

    private okhttp3.Callback callback = new okhttp3.Callback() {

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if (response.isSuccessful()) {
                final String body = response.body().string();

                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Gson gson = new Gson();
                        Type jsonType =
                                new TypeToken<BaseResponse<List<Works>>>() {
                                }.getType();
                        BaseResponse<List<Works>> workBaseResponse =
                                gson.fromJson(body, jsonType);
                        myWorksData = workBaseResponse.getData();
                        myWorksListAdapter = new MyWorksListAdapter(getContext(), R.layout.my_works_list_item, myWorksData);
                        GridLayoutManager llm = new GridLayoutManager(getContext(), 1);
                        rv_my_works_list.setLayoutManager(llm);
                        rv_my_works_list.setAdapter(myWorksListAdapter);
                    }
                });
            }
        }

        @Override
        public void onFailure(@NotNull Call call, @NotNull IOException e) {
            e.printStackTrace();
        }
    };


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        userid = getActivity().getIntent().getIntExtra("userid", 0);
        Log.d("TAG", "onCreateView: " + userid);

        View myView = inflater.inflate(R.layout.fragment_my_works_list, container, false);
        rv_my_works_list = myView.findViewById(R.id.rv_my_works_list);
        refreshData();

        return myView;
    }

    private void refreshData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Request request = new Request.Builder()
                        .url(Constants.MY_WORKS_LIST_URL + "?userid=" + userid)
                        .get().build();
                try {
                    OkHttpClient client = new OkHttpClient();
                    client.newCall(request).enqueue(callback);
                } catch (NetworkOnMainThreadException ex) {
                    ex.printStackTrace();
                }
            }
        }).start();
    }
}